<?php

namespace App\Http\Controllers\Api;

use App\Enums\RightsType;
use App\Exports\DeliveryExport;
use App\Exports\ProductExport;
use App\Http\Controllers\Controller;
use App\Jobs\DownloadsJob;
use App\Models\AgentUser;
use App\Models\Download;
use App\Models\Product;
use Vinlon\Laravel\LayAdmin\Exceptions\AdminException;

class TestController extends Controller
{
    /** 查询用户邀请排名 */
    public function test()
    {
        $product = Product::query()->find(12959);
        if (!$product) {
            throw new AdminException('商品未找到');
        }
        if ($product->rights_type->isNot(RightsType::ENTITY)) {
            throw new AdminException('当前商品没有实物权益');
        }
        /** @var AgentUser $user */
        $user = auth()->user();

        $file = $product->name . '购买记录' . date('YmdHis') . uniqid() . '.xls';

        $download = Download::create([
            'agent_id' => 3,
            'user_id' => 3,
            'name' => $file,
            'url' => 'storage/' . $file,
            'status' => 0,
            'model' => DeliveryExport::class,
            'request' => json_encode(request()->all()),
        ]);

        $job = new DownloadsJob($download);

        $job->handle();

//        dispatch(new DownloadsJob($download));

        return 1;
        $file = '发行商品' . date('YmdHis') . uniqid() . '.xls';

        $download = Download::create([
            'agent_id' => 3,
            'user_id' => 3,
            'name' => $file,
            'url' => 'storage/' . $file,
            'status' => 0,
            'model' => ProductExport::class,
            'request' => json_encode(request()->all()),
        ]);

        $job = new DownloadsJob($download);

        $job->handle();
        /* @var AgentUser $user */

        return 1;
    }
}
